//--------------------------------------------------- // Purpose: To implement the game "keep away chess" // using 2D arrays and functions. // Authors: John Gauch //--------------------------------------------------- #include using namespace std; // Program constants const int ROWS = 8; const int COLS = 8; //--------------------------------------------------- // Print the 2D game board //--------------------------------------------------- void print_board(char board[ROWS][COLS]) { // Print values cout << " "; for (int col = 0; col < COLS; col++) cout << col << " "; cout << endl; // Print line cout << " +"; for (int col = 0; col < COLS; col++) cout << "---+"; cout << "\n"; // Print board for (int row = 0; row < ROWS; row++) { // Print values cout << " " << row << " | "; for (int col = 0; col < COLS; col++) cout << board[row][col] << " | "; cout << endl; // Print line cout << " +"; for (int col = 0; col < COLS; col++) cout << "---+"; cout << "\n"; } } //--------------------------------------------------- // Main program //--------------------------------------------------- int main() { // Define game board char board[ROWS][COLS]; for (int row = 0; row < ROWS; row++) for (int col = 0; col < COLS; col++) board[row][col] = ' '; // Print game board print_board(board); // Get user input int my_row, my_col; cout << "Enter row and col: "; cin >> my_row >> my_col; // Fill in row for (int col = 0; col < COLS; col++) board[my_row][col] = 'c'; // Fill in column for (int row = 0; row < ROWS; row++) board[row][my_col] = 'r'; // Fill in diagonals (long way) for (int count = 0; count < ROWS; count++) { if (my_row+count < ROWS && my_col+count < COLS) board[my_row+count][my_col+count] = '1'; if (my_row+count < ROWS && my_col-count >= 0) board[my_row+count][my_col-count] = '2'; if (my_row-count >= 0 && my_col+count < COLS) board[my_row-count][my_col+count] = '3'; if (my_row-count >= 0 && my_col-count >= 0) board[my_row-count][my_col-count] = '4'; } // Fill in diagonals (short way) for (int row = 0; row < ROWS; row++) for (int col = 0; col < COLS; col++) if (row+col == my_row+my_col || row-col == my_row-my_col) board[row][col] = 'd'; // Print game board board[my_row][my_col] = 'X'; print_board(board); return 0; }